package com.karlis.service;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
 
import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.karlis.domain.Item;
import com.karlis.domain.Warehouse;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("itemService")
@Transactional
public class ItemService {

	protected static Logger logger = Logger.getLogger("service");
	  
	 @Resource(name="sessionFactory")
	 private SessionFactory sessionFactory;
	 
	 /**
	  * Retrieves all items 
	  */
	 public List<Item> getAll(Integer warehouseId) {
	  logger.debug("Retrieving all item ");
	   
	  // Retrieve session from Hibernate
	  Session session = sessionFactory.getCurrentSession();
	   
	  // Create a Hibernate query (HQL)
	  Query query = session.createQuery("FROM Warehouse as p LEFT JOIN FETCH  p.items WHERE p.id="+warehouseId);
	   
	  Warehouse warehouse = (Warehouse) query.uniqueResult();
	   
	  // Retrieve all
	  return  new ArrayList<Item>(warehouse.getItems());
	 }
	 
	 /**
	  * Retrieves all item 
	  */
	 public List<Item> getAll() {
	  logger.debug("Retrieving all item ");
	   
	  // Retrieve session from Hibernate
	  Session session = sessionFactory.getCurrentSession();
	   
	  // Create a Hibernate query (HQL)
	  Query query = session.createQuery("FROM  Item");
	   
	  // Retrieve all
	  return  query.list();
	 }
	 
	 /**
	  * Retrieves a single item 
	  */
	 public Item get( Integer id ) {
	  // Retrieve session from Hibernate
	  Session session = sessionFactory.getCurrentSession();
	   
	  // Retrieve existing item 
	  Item item = (Item) session.get(Item.class, id);
	   
	  // Persists to db
	  return item;
	 }
	 
	 /**
	  * Adds a new item 
	  */
	 public void add(Integer warehouseId, Item item) {
	  logger.debug("Adding new item ");
	   
	  // Retrieve session from Hibernate
	  Session session = sessionFactory.getCurrentSession();
	  
	  // Persists to db
	  session.save(item);
	   
	  // Add to person as well
	  // Retrieve existing person via id
	  Warehouse existingWarehouse = (Warehouse) session.get(Warehouse.class, warehouseId);
	   
	  // Assign updated values to this person
	  existingWarehouse.getItems().add(item);
	 
	  // Save updates
	  session.save(existingWarehouse);
	 }
	 
	 /**
	  * Deletes an existing item 
	  */
	 public void delete(Integer id) {
	  logger.debug("Deleting existing item ");
	   
	  // Retrieve session from Hibernate
	  Session session = sessionFactory.getCurrentSession();
	   
	     // Delete reference to foreign key item  first
	  // We need a SQL query instead of HQL query here to access the third table
	     Query query = session.createSQLQuery("DELETE FROM WAREHOUSE_ITEM " +
	       "WHERE ITEMS_ID="+id);
	      
	     query.executeUpdate();
	      
	  // Retrieve existing item 
	  Item item = (Item) session.get(Item.class, id);
	   
	  // Delete 
	  session.delete(item);
	 }
	 
	 /**
	  * Edits an existing item 
	  */
	 public void edit(Item item) {
	  logger.debug("Editing existing item");
	   
	  // Retrieve session from Hibernate
	  Session session = sessionFactory.getCurrentSession();
	   
	  // Retrieve existing item  via id
	  Item existingItem = (Item) session.get(Item.class, item.getId());
	   
	  // Assign updated values to this item 
	  existingItem.setNumber(item.getNumber());
	  existingItem.setName(item.getName());
	 
	  // Save updates
	  session.save(existingItem);
	 }
	
}
